package org.mparts.client.activity;

import java.util.ArrayList;
import java.util.List;

import org.mparts.client.content.Strings;
import org.mparts.client.place.CatalogPlace;
import org.mparts.client.ui.ICatalogView;
import org.mparts.client.ui.ICatalogView.ICatalogPresenter;
import org.mparts.client.ui.editor.IPartitemEditorView;
import org.mparts.client.ui.editor.IPartitemEditorView.IPartitemEditorPresenter;
import org.mparts.client.ui.events.ReloadPartitemListEvent;
import org.mparts.client.ui.events.ReloadPartitemListEventHandler;
import org.mparts.client.ui.events.ShowPartitemEvent;
import org.mparts.client.ui.events.ShowPartitemEventHandler;
import org.mparts.client.utils.Utils;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.AutoProxy;
import org.mparts.shared.proxy.EngineProxy;
import org.mparts.shared.proxy.PartgroupProxy;
import org.mparts.shared.proxy.PartitemProxy;
import org.mparts.shared.request.MpartsRequest;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

public class CatalogActivity extends AbstractMpartsActivity implements
		ICatalogPresenter {

	private final EventBus eventBus;
	private final ICatalogView display;
	private final PlaceController placeController;

	private String searchCriteria = "";

	// private List<PartgroupProxy> partgroupList;
	// private List<AutoProxy> autoList;

	private IPartitemEditorPresenter editorActivitity;
	private Strings strings;

	@Inject
	public CatalogActivity(MpartsRequestFactory rf, EventBus eventBus,
			ICatalogView display, PlaceController placeController,
			IPartitemEditorPresenter editorActivitity, Strings strings) {
		this.eventBus = eventBus;
		this.display = display;
		this.placeController = placeController;
		this.rf = rf;
		this.editorActivitity = editorActivitity;
		this.strings = strings;

		bind();
		if ( Utils.isManager() ) {
			bindManaged();
		}

	}

	private void bindManaged() {
		display.getAdd().addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (editorActivitity != null) {
					// PartitemProxy item =
					// rf.mpartsRequest().create(PartitemProxy.class);
					editorActivitity.editPartitem(null);
				}
			}
		});

		display.getEdit().addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (editorActivitity != null) {
					PartitemProxy item = display.getGrid().getSelectionModel()
							.getSelectedItem();
					editorActivitity.editPartitem(item);
				}
			}
		});

		display.getDelete().addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (editorActivitity != null) {
					final PartitemProxy item = display.getGrid()
							.getSelectionModel().getSelectedItem();
					if (item != null) {
						final ConfirmMessageBox box = new ConfirmMessageBox(
								strings.confirm(), strings.confirmDeleteTitle());
						box.addHideHandler(new HideHandler() {

							@Override
							public void onHide(HideEvent event) {
								Dialog btn = (Dialog) event.getSource();
								Log.debug("Button '{0}' pressed", btn
										.getHideButton().getText());
								if (box.getHideButton() == box
										.getButtonById(PredefinedButton.YES
												.name())) {
									MpartsRequest req = rf.mpartsRequest();
									req.deletePartitem(req.edit(item));
									req.fire(new Receiver<Void>() {

										@Override
										public void onSuccess(
												Void response) {
											display.getGrid()
													.getLoader().load();
										}
									});
								}
							}
						});
						box.show();
					}
				}
			}
		});
	}
	
	private void bind() {
		
//		eventBus.addHandler(ShowPartitemEvent.TYPE,
//				new ShowPartitemEventHandler() {
//
//					@Override
//					public void onShowPartitem(ShowPartitemEvent showEvent) {
//						display.mask(showEvent.isState());
//					}
//				});
		eventBus.addHandler(ReloadPartitemListEvent.TYPE,
				new ReloadPartitemListEventHandler() {
					
					@Override
					public void onReload(ReloadPartitemListEvent saveEvent) {
						display.getGrid().getLoader().load();
					}
				});
	}

	@Override
	public void goTo(Place place) {
		placeController.goTo(place);

	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		display.setPresenter(this);
		panel.setWidget(display.asWidget());
	}

	public void init(CatalogPlace place) {
		this.searchCriteria = place.getPlaceName();
		display.setSearchCriteria(searchCriteria);
	}

	@Override
	public void loadLists(ListStore<PartgroupProxy> partgroupStore,
			ListStore<AutoProxy> autoStore, final ListStore<String> engineStore) {
		if ( Utils.getEngineList() == null ) {
			rf.mpartsRequest().findAllEngines().fire(new Receiver<List<EngineProxy>>() {

				@Override
				public void onSuccess(List<EngineProxy> response) {
					Utils.setEngineList(response);
					ArrayList<String> eList = new ArrayList<String>();
					for( EngineProxy engine : response ) {
						if (!eList.contains(engine.getName()) )
						eList.add(engine.getName());
					}
					engineStore.addAll(eList);
				}
				
			});
		}
		loadLists(partgroupStore, autoStore);
	}

	// @Override
	// public void initEditorActivity(IPartitemEditorView view) {
	// if ( view != null ) {
	// editorActivitity = new PartitemActivity(rf, eventBus, view);
	// }
	// }
	//

}
